﻿using System;
using System.Collections.Generic;
using System.Text;
using AspNetCs.IDAL;
using AspNetCs.Model;
using System.Data;
using System.Data.SqlClient;
using AspNetCs.Common;

namespace AspNetCs.SQLServerDAL
{
    public class FriendSite_DAL : IFriendSite
    {
        #region IFriendSite 成员

        public int Add(FriendSite_Model model)
        {
            int result = 0;
            db Conn = new db();
            try
            {
                if (model == null)
                {
                    return -1;
                }

                db.ParameterArry[] Par = new db.ParameterArry[13];


                Par[0].dbType = SqlDbType.Int;
                Par[0].parName = "@LinkType";
                Par[0].objValue = model.LinkType;
                Par[0].size = 4;
                Par[1].dbType = SqlDbType.NVarChar;
                Par[1].parName = "@SiteName";
                Par[1].objValue = model.SiteName;
                Par[1].size = 100;
                Par[2].dbType = SqlDbType.NVarChar; Par[2].parName = "@SiteUrl"; Par[2].objValue = model.SiteUrl;
                Par[3].dbType = SqlDbType.NVarChar; Par[3].parName = "@SiteIntro"; Par[3].objValue = model.SiteIntro;
                Par[4].dbType = SqlDbType.NVarChar; Par[4].parName = "@logoUrl"; Par[4].objValue = model.LogoUrl;
                Par[5].dbType = SqlDbType.NVarChar; Par[5].parName = "@SiteAdmin"; Par[5].objValue = model.SiteAdmin;
                Par[6].dbType = SqlDbType.NVarChar; Par[6].parName = "@Email"; Par[6].objValue = model.Email;
                Par[7].dbType = SqlDbType.NVarChar; Par[7].parName = "@SitePassWord"; Par[7].objValue = model.SitePwd;
                Par[8].dbType = SqlDbType.Int; Par[8].parName = "@IsGood"; Par[8].objValue = model.IsGood;
                Par[9].dbType = SqlDbType.Int; Par[9].parName = "@IsCheck"; Par[9].objValue = model.IsCheck;
                Par[10].dbType = SqlDbType.Int; Par[10].parName = "@Orders"; Par[10].objValue = model.Orders;
                Par[11].dbType = SqlDbType.Int; Par[11].parName = "@TypeId"; Par[11].objValue = model.TypeId;
                Par[12].dbType = SqlDbType.VarChar; Par[12].parName = "@Channel"; Par[12].objValue = model.Channel;
                string Sql = "Insert into FriendSite(LinkType,SiteName,SiteUrl,SiteIntro,logoUrl,SiteAdmin,Email,SitePassWord,IsGood,IsCheck,Orders,typeid,channel) values(@LinkType,@SiteName,@SiteUrl,@SiteIntro,@logoUrl,@SiteAdmin,@Email,@SitePassWord,@IsGood,@IsCheck,@Orders,@TypeId,@Channel)";
                result = Conn.ExcSql(Sql, Par);


            }
            catch
            {
                result = 0;
            }
            Conn.close();
            return result;
        }

        public int Delete(string filter)
        {
            db Conn = new db();

            int result = 0;
            try
            {
                string sql = "delete  From FriendSite where id<>0 " + filter;
                result = Conn.ExcSql(sql);
            }
            finally
            {
                result = -1;

            }

            return result;
        }

        public int update(FriendSite_Model model, string filter)
        {
            int result = 0;
            db Conn = new db();
            try
            {
                if (model == null)
                {
                    return -1;
                }

                db.ParameterArry[] Par = new db.ParameterArry[11];


                Par[0].dbType = SqlDbType.Int;
                Par[0].parName = "@LinkType";
                Par[0].objValue = model.LinkType;
                Par[0].size = 4;
                Par[1].dbType = SqlDbType.NVarChar;
                Par[1].parName = "@SiteName";
                Par[1].objValue = model.SiteName;
                Par[1].size = 100;
                Par[2].dbType = SqlDbType.NVarChar; Par[2].parName = "@SiteUrl"; Par[2].objValue = model.SiteUrl;
                Par[3].dbType = SqlDbType.NVarChar; Par[3].parName = "@SiteIntro"; Par[3].objValue = model.SiteIntro;
                Par[4].dbType = SqlDbType.NVarChar; Par[4].parName = "@logoUrl"; Par[4].objValue = model.LogoUrl;
                Par[5].dbType = SqlDbType.NVarChar; Par[5].parName = "@SiteAdmin"; Par[5].objValue = model.SiteAdmin;
                Par[6].dbType = SqlDbType.NVarChar; Par[6].parName = "@Email"; Par[6].objValue = model.Email;
                Par[7].dbType = SqlDbType.NVarChar; Par[7].parName = "@SitePassWord"; Par[7].objValue = model.SitePwd;
                Par[8].dbType = SqlDbType.Int; Par[8].parName = "@IsGood"; Par[8].objValue = model.IsGood;
                Par[9].dbType = SqlDbType.Int; Par[9].parName = "@IsCheck"; Par[9].objValue = model.IsCheck;
                Par[10].dbType = SqlDbType.Int; Par[10].parName = "@Orders"; Par[10].objValue = model.Orders;

                string Sql = "Update FriendSite Set LinkType=@LinkType,SiteName=@SiteName,SiteUrl=@SiteUrl,SiteIntro=@SiteIntro,logoUrl=@logoUrl,SiteAdmin=@SiteAdmin,Email=@Email,SitePassWord=@SitePassWord,IsGood=@IsGood,IsCheck=@IsCheck,Orders=@Orders where id<>0 " + filter;
                result = Conn.ExcSql(Sql, Par);


            }
            catch
            {
                result = 0;
            }
            Conn.close();
            return result;






        }

        public FriendSite_Model GetModel(DataRow dr)
        {
            FriendSite_Model model = new FriendSite_Model();
            if (dr["id"] != null) { model.Id = StringHelper.isNumber_int(dr["id"].ToString()); }
            if (dr["LinkType"] != null) { model.LinkType = StringHelper.isNumber_int(dr["LinkType"].ToString()); }
            if (dr["SiteName"] != null) { model.SiteName = dr["SiteName"].ToString(); }
            if (dr["SiteUrl"] != null) { model.SiteUrl = dr["SiteUrl"].ToString(); }
            if (dr["SiteIntro"] != null) { model.SiteIntro = dr["SiteIntro"].ToString(); }
            if (dr["logoUrl"] != null) { model.LogoUrl = dr["logoUrl"].ToString(); }
            if (dr["SiteAdmin"] != null) { model.SiteAdmin = dr["SiteAdmin"].ToString(); }
            if (dr["Email"] != null) { model.Email = dr["Email"].ToString(); }
            if (dr["SitePassWord"] != null) { model.SitePwd = dr["SitePassWord"].ToString(); }
            if (dr["Addtime"] != null) { model.Addtime = Convert.ToDateTime(dr["Addtime"].ToString()); }
            if (dr["IsGood"] != null) { model.IsGood = StringHelper.isNumber_int(dr["IsGood"].ToString()); }
            if (dr["IsCheck"] != null) { model.IsCheck = StringHelper.isNumber_int(dr["IsCheck"].ToString()); }

            if (dr["channel"] != null) { model.Channel = dr["channel"].ToString(); }
            if (dr["typeid"] != null) { model.TypeId = StringHelper.isNumber_int(dr["typeid"].ToString()); }
            return model;
        }

        public FriendSite_Model GetModel(int id)
        {
            FriendSite_Model model = new FriendSite_Model();

            db.ParameterArry[] par = new db.ParameterArry[1];
            db Cn = new db();
            par[0].parName = "@id";
            par[0].dbType = SqlDbType.Int;
            par[0].objValue = id;
            par[0].size = 4;
            DataTable dt = Cn.GetSql_DataTable("Select Top 1  * from FriendSite where id= @id", par);
            foreach (DataRow dr in dt.Rows)
            {
                model = GetModel(dr);
            }
            Cn.close();
            return model;
        }

        #endregion
    }
}
